package my.ezbart;

import org.xmlvm.iphone.CGRect;
import org.xmlvm.iphone.NSIndexPath;
import org.xmlvm.iphone.UIAlertView;
import org.xmlvm.iphone.UIButton;
import org.xmlvm.iphone.UIButtonType;
import org.xmlvm.iphone.UIColor;
import org.xmlvm.iphone.UIControl;
import org.xmlvm.iphone.UIControlDelegate;
import org.xmlvm.iphone.UIControlEvent;
import org.xmlvm.iphone.UIImage;
import org.xmlvm.iphone.UILabel;
import org.xmlvm.iphone.UIScreen;
import org.xmlvm.iphone.UITableView;
import org.xmlvm.iphone.UITableViewCell;
import org.xmlvm.iphone.UITableViewController;
import org.xmlvm.iphone.UITableViewDataSource;
import org.xmlvm.iphone.UITableViewDelegate;
import org.xmlvm.iphone.UITableViewStyle;
import org.xmlvm.iphone.UIView;
import org.xmlvm.iphone.UIViewController;

/**
 * @author Cecil Holmes
 * @version 0.1
 * 
 * TripPlanner.java (1st tab on the tab bar)
 * 
 * First View Controller that display at application start. User can set their trip origin, destination
 * and time of departure or arrival.
 * 
 * Holds a Table View with cells that act as buttons linking to Select Station view and 
 * Select Time view. Data from these views is returned to the Trip Planner table and 
 * displayed in the appropriate cell then the Plan Trip button sends the information to the
 * view trip screen.
 * 
 */
public class TripPlanner extends UIViewController {
	private UIView root = new UIView(UIScreen.mainScreen().getApplicationFrame());
	private UIButton planTripButton = UIButton.buttonWithType(UIButtonType.RoundedRect);
	private final static String[] tripData = { "Select Origin", "Select Destination", "Select Time"};
	private UITableViewController tc = new UITableViewController(UITableViewStyle.Grouped);
	private UITableView buttons = tc.getTableView();
	private UILabel origin = new UILabel(new CGRect(10, 10, UIScreen.mainScreen().getBounds().size.width, 30));
	private UILabel destination = new UILabel(new CGRect(10, 95, UIScreen.mainScreen().getBounds().size.width, 30));
	private UILabel time = new UILabel(new CGRect(10, 180, UIScreen.mainScreen().getBounds().size.width, 30));

    public TripPlanner() {
        /*
         * The title is used by the UITabBarController as the caption for
         * the tab.
         */
        this.setTitle("Trip Planner");
        
        /*
         * The following statement associates an icon with the tab.
         * Ordinarily each tab would have an individual icon.
         */
        this.getTabBarItem().setImage(UIImage.imageNamed("55-network.png"));
    }
    
    public void setLabels(String newLabel, int type) {
    	
    	if (type == 0) //origin
    		this.origin.setText(newLabel);
    	if (type == 1) //destination
    		this.destination.setText(newLabel);
    	if (type == 2) //time
    		this.time.setText(newLabel);
    }
    
    public void setOptions(String newOption, int type) {
    	
    	if (type == 0) //origin
    		TripPlanner.tripData[0] = newOption;
    	else if (type == 1) //destination
    		TripPlanner.tripData[1] = newOption;
    
    	else if (type == 2) //time
    		TripPlanner.tripData[2] = newOption;
    	
    	this.buttons.reloadData();
    }
    
    public class TableDataSource extends UITableViewDataSource {

		@Override
		public UITableViewCell cellForRowAtIndexPath(UITableView view, NSIndexPath index) {
            UITableViewCell tc = new UITableViewCell();
 
            UILabel detailLabel = tc.getTextLabel();
            detailLabel.setText(tripData[index.getRow()]);

            return tc;
		}

		@Override
		public int numberOfRowsInSection(UITableView view, int section) {
			//origin, destination, time
			return 3;
		}
    	
    }

    public class TableDelegate extends UITableViewDelegate {
    	
    	public void didSelectRowAtIndexPath(UITableView tableview, NSIndexPath index) {
            
            if (index.getRow() == 0) {
            	getNavigationController().pushViewController(new SelectStation(0), true);
            } else if (index.getRow() == 1) {
            	getNavigationController().pushViewController(new SelectStation(1), true);
            } else if (index.getRow() == 2) {
            	getNavigationController().pushViewController(new SelectTime(), true);
            }
        }
    } 
    
    @Override
    public void loadView() {
    	root.setBackgroundColor(UIColor.groupTableViewBackgroundColor);
    	
    	buttons.setBounces(false);
    	buttons.setScrollEnabled(false);
        buttons.setFrame(new CGRect(0,0, UIScreen.mainScreen().getApplicationFrame().size.width, 
        		buttons.getFrame().size.height));
    	buttons.setDataSource(new TableDataSource());
    	buttons.setDelegate(new TableDelegate());
        
    	//Set up Plan Trip button
    	planTripButton.setTitle("Plan Trip", 0);
    	planTripButton.setFrame(new CGRect(10, 160, UIScreen.mainScreen().getBounds().size.width-20, 40));
    	planTripButton.addTarget(new UIControlDelegate() {
    		
            public void raiseEvent(UIControl uic, int i) {

            	if (TripPlanner.tripData[0].contains("Select Origin") && TripPlanner.tripData[1].contains("Select Destination")) {
            		UIAlertView alert = new UIAlertView("Oops...", "Please select an origin and a destination.", null, "Dismiss");
            		alert.show();
            	} else if (TripPlanner.tripData[0].contains("Select Origin")) {
            		UIAlertView alert = new UIAlertView("Oops...", "Please select an origin.", null, "Dismiss");
            		alert.show();
            	} else if (TripPlanner.tripData[1].contains("Select Destination")) {
            		UIAlertView alert = new UIAlertView("Oops...", "Please select a destination.", null, "Dismiss");
            		alert.show();
            	} else if (TripPlanner.tripData[2].contains("Select Time")) {
            		UIAlertView alert = new UIAlertView("Oops...", "Please select a time.", null, "Dismiss");
            		alert.show();
            	} else {	
            		getNavigationController().pushViewController(new ViewTrip(tripData), true);
            	}
            	
            	/*bypass input validation*/
            	//getNavigationController().pushViewController(new ViewTrip(tripData), true);
            }
        }, UIControlEvent.TouchUpInside);

        root.addSubview(buttons);
        root.addSubview(planTripButton);
        
        this.setView(root);
    }
}